package com.whalehearing.tingyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whalehearing.tingyou.common.utils.StringUtils;
import com.whalehearing.tingyou.domain.HearingTranslationRecord;
import com.whalehearing.tingyou.mapper.HearingTranslationRecordMapper;
import com.whalehearing.tingyou.service.IHearingTranslationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 语音转文字记录Service业务层处理
 *
 * @author zengjun-qxm
 * @date 2024-10-29
 */
@Service
public class HearingTranslationRecordServiceImpl extends ServiceImpl<HearingTranslationRecordMapper, HearingTranslationRecord> implements IHearingTranslationRecordService {

    @Autowired
    private HearingTranslationRecordMapper hearingTranslationRecordMapper;

    /**
     * 查询语音转文字记录列表
     *
     * @param hearingTranslationRecord 语音转文字记录
     * @return 语音转文字记录
     */
    @Override
    public List<HearingTranslationRecord> selectHearingTranslationRecordList(HearingTranslationRecord hearingTranslationRecord) {
        List<HearingTranslationRecord> hearingTranslationRecordList = hearingTranslationRecordMapper.selectList(buildQueryWrapper(hearingTranslationRecord));
        return hearingTranslationRecordList;
    }


    private LambdaQueryWrapper<HearingTranslationRecord> buildQueryWrapper(HearingTranslationRecord query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<HearingTranslationRecord> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getRecordTitle()), HearingTranslationRecord::getRecordTitle, query.getRecordTitle());
        lqw.like(StringUtils.isNotBlank(query.getUsername()), HearingTranslationRecord::getUsername, query.getUsername());
        lqw.like(StringUtils.isNotBlank(query.getRecordContent()), HearingTranslationRecord::getRecordContent, query.getRecordContent());
        lqw.eq(query.getConvertedDate() != null, HearingTranslationRecord::getConvertedDate, query.getConvertedDate());
        lqw.eq(query.getUid() != null, HearingTranslationRecord::getUid, query.getUid());
        lqw.orderByDesc(HearingTranslationRecord::getCreateTime);
        lqw.eq(StringUtils.isNotBlank(query.getRemark()), HearingTranslationRecord::getRemark, query.getRemark());
        return lqw;
    }

    /**
     * 根据uid查询用户，使用语音转文字的日期
     *
     * @param uid 用户ID
     * @return 转换日期列表
     */
    @Override
    public List<String> getRecordDateByUid(Integer uid) {
        // 创建查询条件
        LambdaQueryWrapper<HearingTranslationRecord> qw = new LambdaQueryWrapper<>();
        qw.eq(HearingTranslationRecord::getUid, uid);

        // 查询符合条件的记录
        List<HearingTranslationRecord> records = this.list(qw);

        // 定义日期格式化器
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        // 使用 Set 来存储转换后的日期（自动去重）
        Set<String> uniqueDates = new HashSet<>();

        // 遍历查询结果，提取并转换日期字段
        for (HearingTranslationRecord record : records) {
            String formattedDate = dateFormat.format(record.getConvertedDate());
            uniqueDates.add(formattedDate);
        }

        // 将去重后的结果转回列表
        List<String> uniqueDateList = new ArrayList<>(uniqueDates);

        // 对日期列表进行排序（可选）
        Collections.sort(uniqueDateList);

        // 返回去重后的日期列表
        return uniqueDateList;
    }

}
